package com.ysqcould.web.controller.approval;


import com.ysqcould.approval.domain.SupplyDemand;
import com.ysqcould.approval.domain.vo.DemandVo;
import com.ysqcould.approval.service.ISupplyDemandService;
import com.ysqcould.common.annotation.Log;
import com.ysqcould.common.core.controller.BaseController;
import com.ysqcould.common.core.domain.AjaxResult;
import com.ysqcould.common.core.page.TableDataInfo;
import com.ysqcould.common.enums.BusinessType;
import com.ysqcould.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 供需发布Controller
 * 
 * @author ruoyi
 * @date 2022-06-29
 */
@RestController
@RequestMapping("/approval/demand")
public class SupplyDemandController extends BaseController
{
    @Autowired
    private ISupplyDemandService supplyDemandService;

    /**
     * 查询供需发布列表
     */
    @PreAuthorize("@ss.hasPermi('approval:demand:list')")
    @GetMapping("/list")
    public TableDataInfo list(SupplyDemand supplyDemand)
    {
        startPage();
        List<DemandVo> list = supplyDemandService.selectSupplyDemandList(supplyDemand);
        return getDataTable(list);
    }

    /**
     * 导出供需发布列表
     */
    @PreAuthorize("@ss.hasPermi('approval:demand:export')")
    @Log(title = "供需发布", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SupplyDemand supplyDemand)
    {
        List<DemandVo> list = supplyDemandService.selectSupplyDemandList(supplyDemand);
        ExcelUtil<DemandVo> util = new ExcelUtil<DemandVo>(DemandVo.class);
        util.exportExcel(response, list, "供需发布数据");
    }

    /**
     * 获取供需发布详细信息
     */
    @PreAuthorize("@ss.hasPermi('approval:demand:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(supplyDemandService.selectSupplyDemandById(id));
    }

    /**
     * 新增供需发布
     */
    @PreAuthorize("@ss.hasPermi('approval:demand:add')")
    @Log(title = "供需发布", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SupplyDemand supplyDemand)
    {
        supplyDemand.setAsId(getUserId());
        return toAjax(supplyDemandService.insertSupplyDemand(supplyDemand));
    }

    /**
     * 修改供需发布
     */
    @PreAuthorize("@ss.hasPermi('approval:demand:edit')")
    @Log(title = "供需发布", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SupplyDemand supplyDemand)
    {
        return toAjax(supplyDemandService.updateSupplyDemand(supplyDemand));
    }

    /**
     * 删除供需发布
     */
    @PreAuthorize("@ss.hasPermi('approval:demand:remove')")
    @Log(title = "供需发布", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(supplyDemandService.deleteSupplyDemandByIds(ids));
    }
}
